import { getToken } from '../auth';

const base_url = (window as any).config.base_url;
const token = getToken();
const utlis = {
  Percentage: (sum, total, flag) => {
    // flag 是否需要%号
    //flag是true返回百分比字符串 否则返回整数百分比
    total = total > 0 ? total : 1;
    sum = parseFloat(sum);
    total = parseFloat(total);
    if (isNaN(sum) || isNaN(total)) {
      return '-';
    }
    if (flag) {
      return total <= 0 ? '0%' : (Math.round((sum / total) * 10000) / 100.0).toFixed(0) + '%';
    } else {
      return total <= 0 ? '0' : parseInt(Math.round((sum / total) * 10000) / 100.0).toFixed(0);
    }
  },

  groupBy(array, f) {
    const groups = {};
    array.forEach(function (o) {
      const group = f(o);
      groups[group] = groups[group] || [];
      groups[group].push(o);
    });
    return Object.keys(groups).map(function (group) {
      return { key: group, val: groups[group] };
    });
  },
};

export const getImg = (mapId) => {
  return `${base_url}Maps/pic/${mapId}?access_token=${token}`;
};

export default utlis;

export const cust = (name) => {
  //姓名脱敏
  if (name && name != undefined && name != '') {
    const name2 = '';
    if (name.length == 2) {
      name = name.substring(0, 1) + '*';
    }
    if (name.length > 2) {
      name = name.substring(0, 1) + '*' + name.charAt(name.length - 1);
    }
    return name;
  } else {
    return name;
  }
};

export function BrowserSize() {
  window.onresize = null;
  var docEl = document.documentElement,
    clientWidth: number,
    clientHeight: number,
    clientWidth = docEl.clientWidth;
  clientHeight = docEl.clientHeight;
  if (!clientWidth) return;
  if (!docEl.addEventListener) return;
  docEl.style.fontSize = clientWidth / 10 + 'px';
  window.onresize = BrowserSize;
}

export function unBrowserSize() {
  const docEl = document.documentElement;
  if (!docEl.addEventListener) return;
  window.onresize = null;
  // clientWidth = 1920;

  // if (clientWidth < 1920) {
  //   clientWidth = clientWidth;
  // }

  docEl.style.fontSize = 'initial';
}
